import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaField;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
import com.thoughtworks.qdox.model.JavaType;
import com.zwl.util.ReflectUtil;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author buhao
 * @version DemoParser.java, v 0.1 2020-03-22 19:03 buhao
 */
public class DemoParser {
    public static void main(String[] args) throws IOException {
        test1();
    }

    private static void test1() throws IOException {
        // 创建 java 项目 builder 对象
        JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
        // 添加 java 源文件
//        javaProjectBuilder.addSource(new File("E:\\src\\zwl-swagger\\controller-demo\\src\\main\\java\\com\\zwl\\xxx\\controller\\VgroupGrayController.java"));
        javaProjectBuilder.addSourceTree(new File("E:\\src\\zwl-swagger\\controller-demo\\src\\main\\java\\com\\zwl\\xxx\\"));
        // 获得解析后的类
        Collection<JavaClass> classes = javaProjectBuilder.getClasses();
        for (JavaClass javaClass : classes) {
            // 打印类相关信息
            System.out.println("类名:" + javaClass.getName());
            System.out.println("实现了哪些类：" + javaClass.getImplements());
            System.out.println("继承哪个类：" + javaClass.getSuperJavaClass());
            System.out.println("注释：" + javaClass.getComment());


            // 获得方法列表
            List<JavaMethod> methods = javaClass.getMethods();
            for (JavaMethod method : methods) {
                System.out.println("方法名是：" + method.getName());
                System.out.println("方法的 Tags 有哪些：" + method.getTags().stream().map(it -> it.getName() + "->"+ it.getValue()).collect(Collectors.joining("\n")));
                List<JavaType> parameterTypes = method.getParameterTypes();
                System.out.println("parameterTypes = " + parameterTypes);
                for (JavaType parameterType : parameterTypes) {
//                    String canonicalName = parameterType.getCanonicalName();
//                    System.out.println("canonicalName = " + canonicalName);
                    String binaryName = parameterType.getBinaryName();
                    System.out.println("binaryName = " + binaryName);
                    Object name = ReflectUtil.getFieldValue(parameterType, "name");
                    System.out.println("参数类型简称 name = " + name);

                }
                System.out.println("方法的参数有哪些：" + method.getParameters());
                System.out.println("方法的返回值有哪些：" + method.getReturns());
                String comment = method.getComment();
                System.out.println("comment = " + comment);
            }

            List<JavaField> fields = javaClass.getFields();
            for (JavaField field : fields) {
                String comment = field.getComment();
                System.out.println("comment = " + comment);
            }
        }
    }


    public static void test2() {
        JavaProjectBuilder builder =  new  JavaProjectBuilder();
        builder.addSourceTree(new File("src/main/java/com/zwl/xxx"));

        //目录下的所有class
        System.out.println("====目录下的所有class====");
        Collection<JavaClass> classes = builder.getClasses();
        System.out.println(classes + "\n");

        //根据class全限定名获取
        JavaClass javaClass = builder.getClassByName("com.zwl.xxx.controller.VgroupGrayController");

        //获取类的注释
        System.out.println("====获取类的注释====");
        System.out.println(javaClass.getComment());
        List<DocletTag> classTags = javaClass.getTags();
        classTags.forEach(item -> {
            System.out.println(item.getName() + "" + item.getValue());
        });
        System.out.println("\n");

        //获取继承的父类
        System.out.println("====获取继承的父类====");
        System.out.println(javaClass.getSuperClass() + "\n");

        //获取接口
        System.out.println("====获取接口====");
        System.out.println(javaClass.getImplements() + "\n");

        //获取方法
        System.out.println("====获取方法====");
        List<JavaMethod> methods = javaClass.getMethods();
        System.out.println(methods + "\n");

        //get Login方法
        JavaMethod javaMethod = methods.get(0);
        //方法返回类型
        System.out.println("====获取方法返回类型====");
        System.out.println(javaMethod.getReturns() + "\n");
        //获取参数
        System.out.println("====获取参数====");
        List<JavaParameter> parameters = javaMethod.getParameters();
        System.out.println(parameters);
        //获取参数类型
        //get userName参数
        JavaParameter parameter = parameters.get(0);
        //参数名称
        System.out.println(parameter.getName());
        //参数类型
        System.out.println(parameter.getType() + "\n");
        System.out.println("====获取方法注释====");
        //获取方法注释
        System.out.println(javaMethod.getComment());
        //获取参数备注
        List<DocletTag> tags  =javaMethod.getTags();
        tags.forEach(item -> {
            System.out.println(item.getName() + ":" + item.getValue());
        });
    }

}